﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace System
{
    /// <summary>
    /// 为一般集合类所做的扩展
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// 字符串不是Null且Empty
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool IsNotEmpty(this string data)
        {
            bool result = false;

            if (data != null)
                result = (string.IsNullOrEmpty(data) == false);

            return result;
        }

        /// <summary>
        /// 字符串是否为Null或Empty
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string data)
        {
            return string.IsNullOrEmpty(data);
        }

        /// <summary>
        /// 字符串是否为Null、Empty和WhiteSpace
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string data)
        {
            return string.IsNullOrWhiteSpace(data);
        }

        /// <summary>
        /// 字符串不是Null、Empty和WhiteSpace
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool IsNotWhiteSpace(this string data)
        {
            bool result = false;

            if (data != null)
                result = (string.IsNullOrWhiteSpace(data) == false);

            return result;
        }

        /// <summary>
        /// 格式化字符串
        /// today = "今天是：{0:yyyy年MM月dd日 星期ddd}".FormatWith(DateTime.Today);
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string FormatWith(this string format, params object[] args)
        {
            return string.Format(format, args);
        }

        /// <summary>
        /// 是否匹配模式
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsMatch(this string s, string pattern)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, pattern);
        }

        /// <summary>
        /// 匹配字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string Match(this string s, string pattern)
        {
            if (s == null) return "";
            return Regex.Match(s, pattern).Value;
        }

        /// <summary>
        /// 是否为Int
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInt(this string s)
        {
            int i;
            return int.TryParse(s, out i);
        }

        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int ToInt(this string s)
        {
            try
            {
                return int.Parse(s);
            }
            catch
            {
                return default(int);
            }
        }

        /// <summary>
        /// 转换为Camel格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToCamel(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToLower() + s.Substring(1);
        }

        /// <summary>
        /// 转换为Pascal格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToPascal(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToUpper() + s.Substring(1);
        }

        /// <summary>
        /// 将布尔的字符串表示形式转换为它的等效布尔值。一个指示转换是否成功的返回值
        /// </summary>
        /// <param name="input">包含要转换的数字的字符串</param>
        /// <returns>如果 input 转换成功，则为返回转换后的布尔值；否则为 false</returns>
        public static bool IsBoolean(this string input)
        {
            if (input.Equals("true", StringComparison.CurrentCultureIgnoreCase)) return true;
            else if (input.Equals("false", StringComparison.InvariantCultureIgnoreCase)) return false;
            else return false;  
        }

        /// <summary>
        /// 将布尔的字符串表示形式转换为它的等效布尔值
        /// </summary>
        /// <param name="input">包含要转换的数字的字符串</param>
        /// <returns>如果 input 转换成功，则为返回转换后的布尔值；否则为 false</returns>
        public static bool ToBoolean(this string input)
        {
            if (input.Equals("true", StringComparison.CurrentCultureIgnoreCase)) return true;
            else if (input.Equals("false", StringComparison.InvariantCultureIgnoreCase)) return false;
            else return false;
        }
    }
}
